using UnityEngine;
using System.Collections.Generic;

// 关键：添加此特性，让脚本在编辑模式下执行
[ExecuteInEditMode]
public class MapWallGenerator : MonoBehaviour {
    public GameObject wallPrefab;
    private List<Vector3> wallPositions = new List<Vector3>();

    // 确保只生成一次（避免重复生成）
    private bool hasGenerated = false;

    void OnEnable() {
        // 只在未生成过且预制体存在时执行
        if (!hasGenerated && wallPrefab != null) {
            GenerateWalls();
            hasGenerated = true; // 标记为已生成
        }
    }

    void GenerateWalls() {
        // （以下是之前的坐标逻辑，不变）
        // 1. 外框墙体坐标
        for (int x = -9; x <= 9; x++) wallPositions.Add(new Vector3(x, 1, 9));
        for (int x = -9; x <= 9; x++) wallPositions.Add(new Vector3(x, 1, -9));
        for (int z = -8; z <= 8; z++) wallPositions.Add(new Vector3(-9, 1, z));
        for (int z = -8; z <= 8; z++) wallPositions.Add(new Vector3(9, 1, z));

        // 2. 内部隔断坐标（省略，同之前）
        int[] upperX = { -7, -6, -5, 5, 6, 7 };
        foreach (int x in upperX) wallPositions.Add(new Vector3(x, 1, 6));
        for (int x = -9; x <= 9; x++) wallPositions.Add(new Vector3(x, 1, 3));
        foreach (int x in upperX) wallPositions.Add(new Vector3(x, 1, -3));
        int[] leftZ = { 5, 4, 2, 1, -1, -2, -4, -5 };
        foreach (int z in leftZ) wallPositions.Add(new Vector3(-6, 1, z));
        foreach (int z in leftZ) wallPositions.Add(new Vector3(6, 1, z));

        // 生成墙体（父物体设为当前对象，方便管理）
        foreach (Vector3 pos in wallPositions) {
            Instantiate(wallPrefab, pos, Quaternion.identity, transform);
        }
    }

    // 可选：添加清空功能，方便重新生成
    [ContextMenu("清空墙体")]
    void ClearWalls() {
        // 删除所有子物体（墙体实例）
        foreach (Transform child in transform) {
            DestroyImmediate(child.gameObject);
        }
        hasGenerated = false; // 重置标记
    }
}